<template>
  <l-layout class="l-tab-page">
    <template #left>
      <l-panel style="padding-right: 0">
        <template #title>
          {{ $t("开发模版分类") }}
          <div class="tree-setting-btn">
            <el-tooltip effect="dark" content="分类管理" placement="top">
              <el-button @click="handleSettingClick" type="text" icon="el-icon-setting"></el-button>
            </el-tooltip>
          </div>
        </template>
        <el-tree
          ref="tree"
          v-loading="treeLoading"
          node-key="id"
          :highlight-current="true"
          :data="schemeTypes"
          @node-click="handleNodeClick"
        >
          <span class="lr-tree-node" slot-scope="{ node }">
            <i class="el-icon-notebook-2"></i>
            {{ node.label }}
          </span>
        </el-tree>
      </l-panel>
    </template>
    <l-panel style="padding-left: 0">
      <template #toolLeft>
        <div class="l-panel--item">
          <el-input
            :placeholder="$t('请输入要查询关键字')"
            @keyup.enter.native="hanleSearch"
            v-model="searchWord"
            size="mini"
          >
            <el-button @click="hanleSearch" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </div>
      </template>
      <l-table
        :columns="columns"
        :dataSource="tableData"
        row-key="f_Id"
        :isPage="true"
        :pageTotal="tableTotal"
        :tablePage.sync="tableCurrentPage"
        @loadPageData="turnTablePage"
      >
        <template v-slot:f_CreateTime="scope">
          {{ lr_dateFormat(scope.row.f_CreateTime) }}
        </template>
        <template v-slot:f_Classify="scope">
          {{ (schemeTypes.find((t) => t.value == scope.row.f_Classify) || {}).label || "" }}
        </template>
        <template v-slot:f_EnabledMark="scope">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="scope.row.f_EnabledMark"
            @change="handleEnableChange(scope.row)"
          >
          </el-switch>
        </template>
        <l-table-btns :btns="tableBtns" @click="lr_handleTableBtnClick"></l-table-btns>
      </l-table>
    </l-panel>

    <!--模版分类-->
    <l-drawer
      :title="$t('模版分类管理')"
      :visible.sync="schemeTypeVisible"
      :showOk="false"
      :showClose="false"
      size="800px"
      @closed="schemeTypeClosed"
    >
      <scheme-type></scheme-type>
    </l-drawer>
  </l-layout>
</template>

<script>
const apiSchemeType = window.$api.codetool.schemeType;
const apiScheme = window.$api.codetool.scheme;

import SchemeType from "../schemeType/index.vue";
export default {
  components: {
    SchemeType,
  },
  data() {
    return {
      schemeTypeVisible: false,
      schemeTypes: [],
      treeCategory: "",

      //查询
      searchWord: "",
      columns: [
        { label: "名称", prop: "f_Name", minWidth: "150" },
        { label: "时间", prop: "f_CreateTime", minWidth: "150" },
        { label: "分类", prop: "f_Classify", width: "120" },
      ],
      tableBtns: [
        { prop: "Load", label: "加载" },
        { prop: "Delete", label: "删除" },
      ],
      tableTotal: 0,
      tablePageSize: 50,
      tableCurrentPage: 1,
      tableData: [],
      currentScheme: {},
      loadScheme: false,

      treeLoading: false,
      schemeVisible: false,
      scheme: "",
      formSchemeData: {
        f_Name: "",
        f_Classify: "",
      },
      schemeRules: {
        f_Name: [{ required: true, message: "请输入模版名称" }],
        f_Classify: [{ required: true, message: "请选择模版分类" }],
      },
    };
  },
  methods: {
    async loadData() {
      await this.getSchemeTypes();
      await this.loadTablePage();
    },

    loadTablePage(isNotFirst) {
      if (!isNotFirst) {
        this.tableCurrentPage = 1;
      }
      let queryData = {
        rows: this.tablePageSize,
        page: this.tableCurrentPage,
        sidx: "F_CreateTime DESC",
        f_Name: this.searchWord,
        f_Classify: this.treeCategory,
      };
      apiScheme
        .getPageProjectId(queryData)
        .then((res) => {
          const data = this.$deepClone(res.data.data);
          this.tableData = data.rows;
          this.tableTotal = data.records;
        })
        .catch(() => {
          this.tableData = [];
        });
    },
    turnTablePage({ rows }) {
      this.tablePageSize = rows;
      this.loadTablePage(true);
    },
    hanleSearch() {
      this.loadTablePage();
    },
    handleNodeClick(node) {
      this.treeCategory = node.value;
      this.tableCurrentPage = 1;
      this.loadTablePage();
    },

    handleDelete($index, row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.tableData.splice($index, 1);
          this.tableTotal--;
          await apiScheme.remove(row.f_Id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleLoad($index, row) {
      this.$emit("load", row);
    },

    async getSchemeTypes() {
      const data = await this.$awaitWraper(apiSchemeType.getListProjectId());
      this.schemeTypes = (data || []).map((t) => {
        return { label: t.f_Name, value: t.f_Value };
      });
    },
    handleSettingClick() {
      this.schemeTypeVisible = true;
    },
    schemeTypeClosed() {
      this.getSchemeTypes();
    },
  },
};
</script>
